package cn.IT.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.Type;
import cn.IT.service.ITypeService;
import cn.IT.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

@Controller()
@Scope("prototype")

public class TypeAction {
    
	private Type type;

	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private ITypeService typeService;

	JsonConfig jsonConfig = new JsonConfig(); 
	
	//获取所有类型
	public String getAllTypes(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Type> types = typeService.getAllTypes();
		if( types != null){
			resultData = "{\"result\": \"success\"" + "," + "\"types\":" + JSONArray.fromObject(types,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
    



	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

}
